package GUitest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JDialogDemo2 extends JFrame{
    public static void main(String[] args) {
        new JDialogDemo2();
    }

    private JDialog dialog = new JDialog(JDialogDemo2.this,"");

    JButton b1 = new JButton("模态对话框");
    JButton b2 = new JButton("非模态对话框");
    JButton b3 = new JButton("确定");

    public JDialogDemo2(){
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog = new JDialog(JDialogDemo2.this, true);
                dialog.setLayout(new FlowLayout()); // 采用流式布局
                JLabel label = new JLabel("这是模态对话框");
                dialog.add(label);
                dialog.add(b3);
                dialog.setTitle("模态对话框");
                dialog.setBounds(80, 80, 100, 100);
                dialog.setVisible(true);
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog = new JDialog(JDialogDemo2.this, false);
                dialog.setLayout(new FlowLayout()); // 采用流式布局
                JLabel label = new JLabel("这是非模态对话框");
                dialog.add(label);
                dialog.add(b3);
                dialog.setTitle("非模态对话框");
                dialog.setBounds(80, 80, 100, 100);
                dialog.setVisible(true);
            }
        });
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        });

        this.setLayout(new FlowLayout());
        this.add(b1);
        this.add(b2);
        this.setTitle("父窗体");
        this.setVisible(true);
        this.setSize(300, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

}
